/**
 * Created by x on 17-8-2.
 */
angular.module('tribalc').controller('rentagreementDetailController',
    ['$scope','$rootScope','Api','RentProtocolModel','$stateParams','$state','Alert', function ($scope,$rootScope,Api,RentProtocolModel,$stateParams,$state,Alert) {

        getRentlistInfo()
        $scope.showRent = false;
        $scope.showNoRent=false;
        $scope.apartmentId=$stateParams.apartmentId;
        $scope.setupSex=setupSex;
        $scope.showNoprotoclinfo=false;
        $scope.showprotoclinfo = false;
        $scope.canclesRentprotocol = canclesRentprotocol;
        $scope.makeSureCanclesRentprotocol = makeSureCanclesRentprotocol;
        $scope.showCanclesRentProtocol =  false

        function getRentlistInfo() {
            var sourceId=$stateParams.apartmentId;
            Api.getRentlistInfo(sourceId,function (result) {
                
                if(result.data != null){
                    $scope.rentName = result.data.rentName;
                    $scope.id = result.data.id
                }
                $rootScope.rentinfoModel=new RentProtocolModel(result.data);

                $scope.rentinfoModel.pictures = $scope.rentinfoModel.pictures?getReallyPicturePaths($scope.rentinfoModel.pictures):null;
                setupRentinfo(result.data)
                
                if(result.data !== null){
                    getRentprotocolInfo(result.data.id)
                }

            });
        }

        function setupSex(sex) {
            return {'MALE':'男','FEMALE':'女'}[sex];
        }


        function setupRentinfo(rentinfo) {

                if (rentinfo !== null){
                    $scope.showRent = true;
                    $scope.showNoRent=false;
                }
                else if(rentinfo === null){
                    $scope.showNoRent=true;
                    $scope.showRent = false;
                }

        }


        function getReallyPicturePaths(pictures) {
                if (!pictures) return [];
                Array.from(pictures).forEach(function (path, index) {
                    pictures[index] = formatReallyPicturePath(path);
                });
                return pictures;
            }

            function formatReallyPicturePath(path) {

                var oss = 'oss://';
                if (path && path.indexOf(oss) !== -1) {
                    path = Api.ALIYUN_URL + path.split(oss)[1];
                }
                return path;
            }


        function getRentprotocolInfo(id) {
            Api.getRentprotocolInfo(id,function (result) {
                   setupProtoclinfo(result.data);
            });

        }

        function setupProtoclinfo(protoclinfo) {

                if (protoclinfo !== null){
                    $scope.showprotoclinfo = true;
                    $scope.showNoprotoclinfo=false;
                }
                else if(protoclinfo === null){
                    $scope.showNoprotoclinfo=true;
                    $scope.showprotoclinfo = false;
                }

        }

        function canclesRentprotocol() {
          $scope.showCanclesRentProtocol =  true
        }
        function makeSureCanclesRentprotocol(){
          if($scope.confirmRentName == $scope.rentName){
            Api.cancleRentprotocol($scope.id,$scope.rentName,function (result) {
              // $state.go('company.companyRentagreementDetail')
              location.reload()
            });
          }else{
            Alert.alertBox("输入租客名字错误")
          }
          
        }
  
     
    }]);
